package com.example.john.zyfypt_408lwn.activity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.view.menu.ActionMenuItemView;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebView;
import android.widget.Toast;

import com.example.john.zyfypt_408lwn.R;
import com.example.john.zyfypt_408lwn.iface.CollectListener;
import com.example.john.zyfypt_408lwn.iface.FocusListener;
import com.example.john.zyfypt_408lwn.model.CollectModel;
import com.example.john.zyfypt_408lwn.model.FocusModel;




public class ViewArticleActivity extends AppCompatActivity {
   // private int resid;
    //private WebView webView;
   // private String BASEURL="http://amicool.neusoft.edu.cn/";
    //@Override
    //protected void onCreate(Bundle savedInstanceState) {
       // super.onCreate(savedInstanceState);
       // setContentView(R.layout.viewarticleactivity);
        //System.out.println("----查看文章详情");

        //resid  = getIntent().getIntExtra("resid",1);

        //webView = (WebView)findViewById(R.id.webview);
        //webView.loadUrl(BASEURL+"article.php/show/index/id/"+resid);
    //}


    private int resid;//资源id
    private int userid;//资源用户id
    private int idolid;
    Context context;
    private WebView webView;
     private String BASEURL="http://amicool.neusoft.edu.cn/";
    private Boolean flagcollect=false;//收藏标志
    private Boolean flagfocus=false;//关注标志
private FocusModel focusModel;
    private CollectModel collectmodel;//收藏modelz
    private SharedPreferences sp;//简单存储
    private String sessionid="";  //sessionid
    FocusListener listener1=new FocusListener() {
        @Override
        public void onResponse(String msg) {
            //获取菜单视图
            ActionMenuItemView item=(ActionMenuItemView)findViewById(R.id.menufocus);
            //根据mode中response返回的字符串区分返回结果
            switch (msg)
            {
                case "2": System.out.println("----关注成功");
                    flagfocus=true;
                    item.setTitle("取消关注");
                    break;
                case "1":
                    System.out.println("----关注失败");
                    break;
                case "5":
                    System.out.println("----取消关注成功");
                    flagfocus=false;
                    item.setTitle("关注");
                    break;
                case "4":
                    System.out.println("----取消关注失败");
                    break;
                case "7":
                    System.out.println("----已关注");
                    flagfocus=true;
                    item.setTitle("取消关注");
                    break;
                case "8":
                    System.out.println("----未关注");
                    flagfocus=false;
                    item.setTitle("关注");
                    break;
                default:
                    Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
            }
        }
        @Override
        public void onFail(String msg) {
            Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
        }
    };

    CollectListener listener=new CollectListener() {
        @Override
        public void onResponse(String msg) {
            //获取菜单视图
            ActionMenuItemView item=(ActionMenuItemView)findViewById(R.id.menucollect);
            //根据mode中response返回的字符串区分返回结果
            switch (msg)
            {
                case "2": System.out.println("----收藏成功");
                    flagcollect=true;
                    item.setTitle("取消收藏");
                    break;
                case "1":
                    System.out.println("----收藏失败");
                    break;
                case "5":
                    System.out.println("----取消收藏成功");
                    flagcollect=false;
                    item.setTitle("收藏");
                    break;
                case "4":
                    System.out.println("----取消收藏失败");
                    break;
                case "7":
                    System.out.println("----已收藏");
                    flagcollect=true;
                    item.setTitle("取消收藏");
                    break;
                case "8":
                    System.out.println("----未收藏");
                    flagcollect=false;
                    item.setTitle("收藏");
                    break;
                default:
                    Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
            }
        }
        @Override
        public void onFail(String msg) {
            Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.viewarticleactivity);
        context=ViewArticleActivity.this;

        resid  = getIntent().getIntExtra("resid",1);//获取传递的资源id
        userid = getIntent().getIntExtra("userid",7);//获取传递的资源用户id
       // idolid=getIntent().getIntExtra("idolid",2);
       // sp=context.getSharedPreferences("login",MODE_PRIVATE);
       // readSP();//读取sessionid





        webView = (WebView)findViewById(R.id.webview);
        webView.loadUrl(BASEURL+"article.php/show/index/id/"+resid);
        sp = context.getSharedPreferences("login", MODE_PRIVATE);
        readSP();//读取sessionid
    }

    private void readSP() {
        sessionid=sp.getString("sessionid",null);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);//加载菜单布局
        focusModel=new FocusModel();
        focusModel.exist("focus",userid,sessionid,listener1);
        collectmodel=new CollectModel();//实例化对象
        collectmodel.exist("article",resid,sessionid,listener);//判断是否收藏
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId())
        {
            case R.id.menucollect:
                //Toast.makeText(this, "文章收藏", Toast.LENGTH_SHORT).show();
                if(flagcollect)//如果已收藏，则调用取消收藏
                {
                    System.out.println("----准备取消收藏");
                    collectmodel.uncollect("article",resid,sessionid,listener);
                }
                else//如果未收藏，则调用收藏
                {
                    System.out.println("----准备收藏");
                    collectmodel.collect("article",resid,sessionid,listener);
                }
                break;
            case R.id.menufocus:
                //Toast.makeText(this, "文章关注", Toast.LENGTH_SHORT).show();
                if(flagfocus)//如果已关注，则调用取消关注
                {
                    System.out.println("----准备取消关注");
                    focusModel.unfocus("focus",userid,sessionid,listener1);
                }
                else//如果未关注，则调用关注
                {
                    System.out.println("----准备关注");
                    focusModel.focus("focus",userid,sessionid,listener1);
                }
                break;
        }
        return super.onOptionsItemSelected(item);
    }

}





